package com.nenglai.admin.handler;

import com.nenglai.common.crud.EditorBody;
import com.nenglai.common.crud.FormBody;
import com.nenglai.common.crud.RemoveBody;
import com.nenglai.common.crud.SearchBody;
import com.nenglai.common.jwt.VerifyToken;
import com.nenglai.common.resource.ResourceManager;
import com.nenglai.common.response.*;
import com.nenglai.common.vo.SearchVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 公共资源控制器
 *
 * @author kongdehui
 * @date 2021/04/21 16:54
 **/
@RestController
@RequestMapping("resource")
public class ResourceController {

    @Resource
    private ResourceManager resourceManager;

    @VerifyToken
    @PostMapping(value = "{apiName}/info")
    public Result<Object> getInfo(@PathVariable("apiName") String apiName, @RequestBody SearchBody searchBody) {
        Object obj = resourceManager.selectOne(apiName, searchBody);
        return ResultGenerator.success(obj);
    }

    @VerifyToken
    @PostMapping(value = "{apiName}/list")
    public ResultData<?> getList(@PathVariable("apiName") String apiName, @RequestBody SearchBody searchBody) {
        SearchVO vo = resourceManager.select(apiName, searchBody);
        return ResultGenerator.searchVO(vo);
    }

    @VerifyToken
    @PostMapping(value = "{apiName}/delete")
    public Result<String> delete(@PathVariable("apiName") String apiName, @RequestBody RemoveBody removeBody) {
        Boolean delete = resourceManager.delete(apiName, removeBody);
        return ResultGenerator.successOrError(delete, "删除成功", "删除失败");
    }

    @VerifyToken
    @PostMapping(value = "{apiName}/add")
    public Result<?> add(@PathVariable("apiName") String apiName, @RequestBody FormBody formBody)
            throws ValidateException {
        Object object = resourceManager.add(apiName, formBody);
        if (object == null) {
            return ResultGenerator.error(ErrorCode.CREATE_ERROR_CODE);
        }
        return ResultGenerator.success(object);
    }

    @VerifyToken
    @PostMapping(value = "{apiName}/update")
    public Result<?> update(@PathVariable("apiName") String apiName, @RequestBody FormBody formBody)
            throws ValidateException {
        Object object = resourceManager.update(apiName, formBody);
        if (object == null) {
            return ResultGenerator.error(ErrorCode.UPDATE_ERROR_CODE);
        }
        return ResultGenerator.success(object);
    }

    @VerifyToken
    @PostMapping(value = "{apiName}/edit")
    public Result<?> edit(@PathVariable("apiName") String apiName, @RequestBody EditorBody editorBody)
            throws ValidateException {
        Boolean bool = resourceManager.edit(apiName, editorBody);
        if (!bool) {
            return ResultGenerator.error(ErrorCode.UPDATE_ERROR_CODE);
        }
        return ResultGenerator.success();
    }

    @VerifyToken
    @PostMapping(value = "{apiName}/configs")
    public Result<Object> configs(@PathVariable("apiName") String apiName) {
        return ResultGenerator.success(resourceManager.configs(apiName));
    }
}
